/**
 * @Author ZhangCuirong
 * @Date 2025/8/6 14:06
 * @description:
 */
public class Solution695 {
    int[][] direction = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    boolean[][] visited;
    public int maxAreaOfIsland(int[][] grid) {
        visited = new boolean[grid.length][grid[0].length];
        int max = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (!visited[i][j]) {
                    max = Math.max(max, DFS(grid, i, j));
                }
            }

        }
        return max;
    }
    public boolean isValid(int[][] grid, int i, int j) {
        return !(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0 || visited[i][j]);
    }
    public int DFS(int[][] grid, int i, int j) {
        if (!isValid(grid, i, j)) {
            return 0;
        }
        visited[i][j] = true;
        int sum = 1;
        for (int k = 0; k < direction.length; k++) {
            int newX = i + direction[k][0];
            int newY = j + direction[k][1];
            sum += DFS(grid, newX, newY);
        }
        return sum;
    }
    public static void main(String[] args) {
        Solution695 solution = new Solution695();
        int[][] grid = {{0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}, {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}};
        int res = solution.maxAreaOfIsland(grid);
        System.out.println(res);
    }
}
